package com.lyhengtongwl.zqsnews.ui.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.WithDrawNowAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.customprovider.GridItemDecoration;
import com.lyhengtongwl.zqsnews.entity.DepositBo;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.entity.WDEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.AppConfig;
import com.lyhengtongwl.zqsnews.utils.CopyUtils;
import com.lyhengtongwl.zqsnews.utils.FTYUtils;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.zqsnews.widget.CenterDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class NewsWithDrawNowActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.iv_title_right)
    ImageView iv_title_right;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_wcService)
    TextView tv_wcService;
    private String balance;
    public static String moneySelected;
    private List<WDEntity> list = new ArrayList<>();
    private WithDrawNowAdapter withDrawNowAdapter;

    @Override
    public void setListen() {

    }

    @OnClick({R.id.tv_copy, R.id.tv_withDrawNow, R.id.rel_1})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_copy://复制文案
                CopyUtils.copyText(this, AppConfig.getInstance(App.getContext()).get("wechat"));
                break;
            case R.id.tv_withDrawNow://立即体现
                double b = Double.parseDouble(FTYUtils.fenToYuan(balance));
                double m = Double.parseDouble(moneySelected);
                if (b < m) {
                    new CenterDialog(R.layout.item_promat_withdraw, NewsWithDrawNowActivity.this).showCheckWithDrawPrompt("可提现余额小于当前提现金额！");
                } else {
                    toWithDraw();
                    /* String authStatus = SPUtils.get(App.getContext(), "authStatus", "") + "";
                     switch (authStatus) {
                     case "0":
                     new CenterDialog(R.layout.prompt_dialog, WithDrawNowActivity.this).showPromptRZ("前去实名认证？");
                     break;

                     //                            startActivity(new Intent(WithDrawNowActivity.this, NewsRecActivity.class));
                     //                            break;
                     case "1":
                     case "2":
                     //                            toWithDraw();
                     break;
                     default:
                     new CenterDialog(R.layout.prompt_dialog, WithDrawNowActivity.this).showPromptRZ("前去实名认证？");
                     break;
                     }**/
                }
                break;
            case R.id.rel_1:
                NewsToastUtils.showToast(App.getContext(), "已经绑定微信！");
                break;

        }
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        iv_title_right.setVisibility(View.GONE);
        new TitleBuilder(this).setTitleText("立即提现")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_withdrawal_now;
    }

    @Override
    public void initView() {
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initData() {
        balance = SPUtils.get(App.getContext(), "balance", "") + "";
        tv_wcService.setText(AppConfig.getInstance(App.getContext()).get("wechat"));
        tv_money.setText(FTYUtils.fenToYuan(balance));
        String depositNum = SPUtils.get(App.getContext(), "depositNum", "") + "";
        String isFirstDeposit = SPUtils.get(App.getContext(), "isFirstDeposit", "") + "";
        withDrawNowAdapter = new WithDrawNowAdapter(list, NewsWithDrawNowActivity.this);
        GridLayoutManager layoutManager = new GridLayoutManager(NewsWithDrawNowActivity.this, 3);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        GridItemDecoration divider = new GridItemDecoration.Builder(Objects.requireNonNull(NewsWithDrawNowActivity.this))
                .setHorizontalSpan(R.dimen.common_vew_column_padding)
                .setVerticalSpan(R.dimen.common_vew_raw_padding)
                .setColorResource(R.color.whiteffffff)
                .setShowLastLine(false)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(withDrawNowAdapter);
        getWithDrawMoney();
        checkIsTell();

    }

    //获取提现金额
    private void getWithDrawMoney() {
        String userId = SPUtils.get(App.getContext(), "userId", "") + "";
        Task.getApiService().getWithDrawMoney(userId).enqueue(new MyCallback<NewsBaseEntity>(NewsWithDrawNowActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {

                if ("1".equals(response.body().getCode())) {
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                    list.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        if (i == 0) {
                            moneySelected = jsonArray.getJSONObject(i).getString("money");
                        }
                        WDEntity wdEntity = new WDEntity();
                        wdEntity.setMoney(jsonArray.getJSONObject(i).getString("money"));
                        list.add(wdEntity);
                    }
                    withDrawNowAdapter.notifyDataSetChanged();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    //查询是否体现被驳回
    private void checkIsTell() {
        String userId = SPUtils.get(App.getContext(), "userId", "") + "";
        Task.getApiService().checkIsTell(userId).enqueue(new MyCallback<NewsBaseEntity>(NewsWithDrawNowActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    //被驳回
                    new CenterDialog(R.layout.item_promat_withdraw, NewsWithDrawNowActivity.this).showCheckWithDrawPrompt(response.body().getMsg());
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                NewsToastUtils.showToast(App.getContext(), "请求失败！");
            }
        });
    }

    //提现
    private void toWithDraw() {
        Dialog dialog = new CenterDialog(R.layout.loading_layout, NewsWithDrawNowActivity.this).showLoad("正在提交审核...");
        DepositBo depositBo = new DepositBo();
        depositBo.setMoney(Double.parseDouble(moneySelected));
        Task.getApiService().toWithDraw(depositBo).enqueue(new MyCallback<NewsBaseEntity>(NewsWithDrawNowActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                try {
                    assert response.body() != null;
                    if ("1".equals(response.body().getCode())) {
                        //表示提现成功
                        dialog.dismiss();
                        new CenterDialog(R.layout.item_promat_withdraw, NewsWithDrawNowActivity.this).showWithDrawPrompt(response.body().getMsg(), "提现成功");

                    } else {
                        dialog.dismiss();
                        new CenterDialog(R.layout.item_promat_withdraw, NewsWithDrawNowActivity.this).showCheckWithDrawPrompt(response.body().getMsg());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    dialog.dismiss();
                }

            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
                NewsToastUtils.showToast(NewsWithDrawNowActivity.this, "请求失败！");
            }
        });
    }
}
